package main

import (
	"fmt"
)

func sender(ch chan string, down chan struct{}) {
	//无缓冲管道，阻塞型，同步通信
	ch <- "hi"
	ch <- "i am"
	ch <- "lwq"
	ch <- "EOF"
	<-down
	close(ch)       //关闭channel
	defer close(ch) //关闭channel
}

func receiver(ch chan string, down chan struct{}) {
	defer func() {
		down <- struct{}{}
	}()
	for v := range ch { //循环读取channel数据，直到channel关闭
		if v == "EOF" {
			return
		}
		fmt.Println(v)
	}
}

func main() {
	ch := make(chan string, 5)
	down := make(chan struct{})
	go sender(ch, down)
	ch := make(chan string)
	down := make(chan struct{})
	go sender(ch)
	go receiver(ch, down)
	<-down
	//time.Sleep(2 * time.Second)
}
